import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//模拟实现线程池
public class Demo_MyThreadPool2 {
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(5);
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    public Demo_MyThreadPool2(int n) throws InterruptedException {
        for(int i = 0; i < n; i++){
            Thread t = new Thread(() -> {
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
}

class TestDemoThreadPool2{
    public static void main(String[] args) throws InterruptedException {
        Demo_MyThreadPool2 threadPool = new Demo_MyThreadPool2(5);
        for(int i = 0; i < 1000; i++){
            int id = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "任务：" + id);
                }
            };
            threadPool.submit(runnable);
        }
    }
}
